package com.yingli.component;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.yingli.component.R;
import com.yingli.component.activity.PermissionsUtils;
import com.yingli.component.activity.entity.Student;
import com.yingli.component.activity.entity.User;
import com.yingli.component.activity.launch.StandardActivity;
import com.yingli.component.activity.transfer.FirstActivity;
import com.yingli.component.activity.utils.NetUtils;
import com.yingli.component.activity.utils.RequestResult;
import com.yingli.component.eventbus.MessageEvent;
import com.yingli.component.receiver.BroadcastActivity;
import com.yingli.component.service.ServiceActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};

    private TextView tvMessage;
    private static final int PERMISSIONRESULT = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        tvMessage = findViewById(R.id.tv_message);
//        //判断权限是否已授予
//        if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
//            //如果不存在，则需要去申请
//            ActivityCompat.requestPermissions(this,permissions,PERMISSIONRESULT);
//        }

        findViewById(R.id.tv_lifecycle).setOnClickListener(v->{
//            startActivity(new Intent(this,SecondActivity.class));
            //隐式跳转
//            Intent intent = new Intent(Intent.ACTION_DIAL);
            Intent intent = new Intent("com.yingli.demo");
            intent.setData(Uri.parse("ylkj://demo.yingli.cn:5860/test"));
//            intent.setAction("com.yingli.sample");
//            intent.addCategory(Intent.CATEGORY_DEFAULT);
//            intent.addCategory("");
            startActivity(intent);
        });

        setResult(10);
        NetUtils.request(new RequestResult() {
            @Override
            public void getAllCars(List<String> cars) {
                Log.e("carsList", cars.toString());
            }
        });

        findViewById(R.id.tv_launcher).setOnClickListener(v->{
            startActivity(new Intent(this, StandardActivity.class));
        });
        findViewById(R.id.btn_broadcast).setOnClickListener(v->{
            startActivity(new Intent(this, BroadcastActivity.class));
        });

        findViewById(R.id.btn_transfer).setOnClickListener(v->{
            startActivity(new Intent(this, FirstActivity.class));
        });
        findViewById(R.id.btn_service).setOnClickListener(v->{
            startActivity(new Intent(this, ServiceActivity.class));
        });

        PermissionsUtils.getInstance().checkPermissions(this, permissions, new PermissionsUtils.IPermissionsResult() {
            //授予所有的权限后的回调
            @Override
            public void passPermissons() {

            }

            //如果有一项权限被拒绝，调用此方法
            @Override
            public void forbitPermissons() {

            }
        });

        Log.i("lifecycle","onCreate");
    }

    /**
     *
     * @param requestCode 请求跳转的标识
     * @param resultCode 数据返回的标识
     * @param data 携带的数据
     */

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    //保存当前实例的状态
    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        User u = new User();
        outState.putSerializable("user",u);
        Student student = new Student();
        outState.putParcelable("student",student);
        outState.putString("index","1");
    }

    //恢复当前实例的运行状态
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        String state = savedInstanceState.getString("index");
    }


    @Override
    protected void onStart() {
        super.onStart();
//        EventBus.getDefault().register(this);
        Log.i("lifecycle","onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("lifecycle","onResume");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("lifecycle","onRestart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("lifecycle","onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("lifecycle","onStop");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MessageEvent event) {
        /* Do something */
        tvMessage.setText(event.message);
        Log.i("postMessage",event.message+"main");

    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        Log.i("lifecycle","onDestroy");
    }

    //configChanges方法设置后，会走此方法
    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    //    //权限申请的结果
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        PermissionsUtils.getInstance().onRequestPermissionsResult(this,requestCode,permissions,grantResults);
////        for (int grantResult : grantResults) {
////            if (grantResult!=PackageManager.PERMISSION_GRANTED){
////                Toast.makeText(this,"权限未授予，无法使用！",Toast.LENGTH_SHORT).show();
////            }
////        }
//
//    }
}